package com.sky.service.impl;

import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO findBusinessData() {
        Map map = new HashMap<>();
        LocalDate date = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
        map.put("begin", beginTime);
        map.put("end", endTime);
        //每天新增用户数量
        Long userCount = userMapper.findByMap(map);
        //每天的营业额
        Double sum = orderMapper.sumByMap(map) == null ? 0.0 : orderMapper.sumByMap(map);
        //每天的有效订单数
        Long count = orderMapper.findValidOrderCount(map);
        //每天的总订单数
        Long orderCount = orderMapper.findOrderCount(map);
        Double completionRate;
        Double unitPrice;
        if (count != null && count != 0) {
            //计算订单完成率
            completionRate = (double) (count / orderCount);
            //计算平均客单价
            unitPrice = sum / count;
        } else {
            completionRate = 0.0;
            unitPrice = 0.0;
        }

        return BusinessDataVO
                .builder()
                .unitPrice(unitPrice)
                .orderCompletionRate(completionRate)
                .validOrderCount(Math.toIntExact(count))
                .turnover(sum)
                .newUsers(Math.toIntExact(userCount))
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public DishOverViewVO findOverviewSetmeals() {
        //已停售套餐数量
        Long discontinued = setmealMapper.findOverviewSetmealsDiscontinued();
        //已启售套餐数量
        Long sold = setmealMapper.findOverviewSetmealsSold();
        return DishOverViewVO
                .builder()
                .discontinued(Math.toIntExact(discontinued))
                .sold(Math.toIntExact(sold))
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO findOverviewDishs() {

        List<Integer> overviewDishs = dishMapper.findOverviewDishs();

        return DishOverViewVO
                .builder()
                //已停售套餐数量
                .discontinued(overviewDishs.get(1))
                //已启售套餐数量
                .sold(overviewDishs.get(0))
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO findOverviewOrders() {
        //查询已完成数量
        Long completedCount = orderMapper.findOverviewOrders(5);
        //查询待接单数量
        Long waitingCount = orderMapper.findOverviewOrders(2);
        //查询已取消数量
        Long cancelledCount = orderMapper.findOverviewOrders(6);
        //查询所有订单
        Long totalCount = orderMapper.findOverviewOrders(null);
        //查询待派送订单
        Long deliveredCount = orderMapper.findOverviewOrders(3);

        return OrderOverViewVO
                .builder()
                .completedOrders(Math.toIntExact(completedCount == null ? 0 : completedCount))
                .deliveredOrders(Math.toIntExact(deliveredCount == null ? 0 : deliveredCount))
                .cancelledOrders(Math.toIntExact(cancelledCount == null ? 0 : cancelledCount))
                .waitingOrders(Math.toIntExact(waitingCount == null ? 0 : waitingCount))
                .allOrders(Math.toIntExact(totalCount == null ? 0 : totalCount))
                .build();
    }
}
